package UserService

import (
	"gdshop-font-api-go/app/request/UserRequest"
	"gdshop-font-api-go/library/response"
	"gdshop-font-api-go/library/tools/db"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/util/gconv"
)

// 编辑
func Edit(r *ghttp.Request) *response.JsonResponse {
	var parames *UserRequest.Edit
	// 转换并验证输入参数是否合规
	if err := r.Parse(&parames); err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}
	// 转换成类似数组的形式
	data := gconv.Map(parames)
	// 添加一下其他的数据
	data["update_at"] = gtime.Timestamp()
	// 更新数据
	_, err := db.GetUnSafaTable(r.GetCtx(), "user").Where(g.Map{
		"id": parames.Id,
	}).Update(data)
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}
	return response.SuccessByRequestMessageData(nil, "成功", g.Map{
		"id": parames.Id,
	})
}
